package com.kaizige.realtor.Controller;

import com.kaizige.realtor.Dao.houseMsgDao;
import com.kaizige.realtor.Dao.houseOwnerDao;
import com.kaizige.realtor.Dao.managerDao;
import com.kaizige.realtor.Dao.tenantDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Controller
public class indexController {
	@Autowired
	houseOwnerDao hod;
	@Autowired
	tenantDao td;
	@Autowired
	managerDao md;
	@Autowired
	houseMsgDao hmd;
	@RequestMapping("/ownIndex")
	public String ownIndex(HttpSession session, Model model){
		List<Map<String, Object>> mapList = hmd.queryHmByOwner((String)session.getAttribute("LoginUser"));
		model.addAttribute("houses",mapList);
		Iterator<Map<String, Object>> it =  mapList.iterator();
		int out = 0;
		int in = 0;
		while(it.hasNext()){
			if(it.next().get("house_status").equals(0)){
				in++;
			}
			else{
				out++;
			}
		}
		model.addAttribute("out",out);
		model.addAttribute("in",in);
		return "/indexs/ownIndex";
	}
	
	@RequestMapping("/tenIndex")
	public String tenIndex(HttpSession session, Model model){
		List<Map<String, Object>> maps = hmd.queryAllWaitMsg();
		model.addAttribute("houses",maps);
		Iterator<Map<String, Object>> it =  maps.iterator();
		int[] types = {0,0,0,0};
		while(it.hasNext()){
			Map<String, Object> next = it.next();
			if(next.get("house_type").equals(0)){
				types[0]++;
			}
			else if(next.get("house_type").equals(1)){
				types[1]++;
			}
			else if(next.get("house_type").equals(2)){
				types[2]++;
			}
			else{
				types[3]++;
			}
		}
		model.addAttribute("types",types);
		return "/indexs/tenIndex";
	}
	
	@RequestMapping("/manIndex")
	public String manIndex(HttpSession session, Model model){
		List<Map<String, Object>> maps = hmd.queryAllMsg();
		model.addAttribute("houses",maps);
		Iterator<Map<String, Object>> it =  maps.iterator();
		int out = 0;
		int in = 0;
		while(it.hasNext()){
			if(it.next().get("house_status").equals(0)){
				in++;
			}
			else{
				out++;
			}
		}
		model.addAttribute("out",out);
		model.addAttribute("in",in);
		return "/indexs/manIndex";
	}
	
}
